package tice.twitterwalk.List;

import java.util.ArrayList;

import tice.twitterwalk.Util.TwitterItem;
import android.content.Context;
import android.database.MatrixCursor;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class ListViewManager {
	
	private ArrayList<TweetsListView> _Views;
	private Context mCtx;
	
	public ListViewManager(Context context){
		_Views = new ArrayList<TweetsListView>(0);
		mCtx = context;
	}
	
	public int GetCurrentListViewIndex(){
		for(int i=0;i<_Views.size();i++){
			if(_Views.get(i).isShown()){
				return i;
			}
		}
		
		return 0;
	}
	
	public TweetsListView AddNewList(ViewGroup parent){
		TweetsListView view = new TweetsListView(mCtx);
		view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		parent.addView(view);
		_Views.add(view);
		return view;
	}
	
	public void setOnItemClickListener(OnItemClickListener l){
		for(int i=0;i<_Views.size();i++){
			_Views.get(i).setOnItemClickListener(l);
		}
	}
	
    public void setOnScrollListener(OnScrollListener l){
		for(int i=0;i<_Views.size();i++){
			_Views.get(i).setOnScrollListener(l);
		}
    }
    
    public void setOnItemLongClickListener(OnItemLongClickListener l){
    	//for(int i=0;i<_Views.size();i++){
    	//	_Views.get(i).setOnItemLongClickListener(l);
    	//}
    }
	
	public TweetsListView GetCurrentListView(){
		int index = GetCurrentListViewIndex();
		return _Views.get(index);
	}
	
	public TweetsCursorAdapter GetCurrentListCursor(){
		int index = GetCurrentListViewIndex();
		return _Views.get(index)._Items;
	}

	public void ReQuery(MatrixCursor c){
		int index = GetCurrentListViewIndex();
		_Views.get(index)._Items.ReQuery(c);
	}

	public void ReQuery(int index, MatrixCursor c){
		_Views.get(index)._Items.ReQuery(c);
	}
	
	public void notifyDataSetChanged(){
		int index = GetCurrentListViewIndex();
		_Views.get(index)._Items.notifyDataSetChanged();
	}

	public void notifyDataSetInvalidated(){
		int index = GetCurrentListViewIndex();
		_Views.get(index)._Items.notifyDataSetInvalidated();
	}
	
	public int getCount(){
		int index = GetCurrentListViewIndex();
		return _Views.get(index)._Items.getCount();
	}
	
	public int GetReadStatus(int position){
		int index = GetCurrentListViewIndex();
		return _Views.get(index)._Items.GetReadStatus(position);
	}
	
	public int GetInt(int position, int cloumIndex){
		int index = GetCurrentListViewIndex();
		return _Views.get(index)._Items.GetInt(position, cloumIndex);
	}
	
	public long GetLong(int position, int cloumIndex){
		int index = GetCurrentListViewIndex();
		return _Views.get(index)._Items.GetLong(position, cloumIndex);
	}
	
	public void CleanReadStatuses(){
		int index = GetCurrentListViewIndex();
		_Views.get(index)._Items.CleanReadStatuses();
	}
	
	public void AddReadStatus(int read){
		int index = GetCurrentListViewIndex();
		_Views.get(index)._Items.AddReadStatus(read);
	}

	public void SetReadStatus(int position, int read){
		int index = GetCurrentListViewIndex();
		_Views.get(index)._Items.SetReadStatus(position, read);
	}
	
	public TwitterItem Get(int position){
		int index = GetCurrentListViewIndex();
		return _Views.get(index)._Items.Get(position);
	}
	
	public TwitterItem GetCurrent(){
		int index = GetCurrentListViewIndex();
		int position = GetCurrentThread();
		return _Views.get(index)._Items.Get(position);
	}
	
	public void SetStartLoadingItem(){
		int index = GetCurrentListViewIndex();
		_Views.get(index)._Items.SetStartLoadingItem();
	}
	
	public void SetLoadingItem(){
		int index = GetCurrentListViewIndex();
		_Views.get(index)._Items.SetLoadingItem();
	}
	
	public void SetScrollState(int state){
		int index = GetCurrentListViewIndex();
		_Views.get(index)._Items.SetScrollState(state);
	}
	
	public void setSelection(int position){
		int index = GetCurrentListViewIndex();
		_Views.get(index).setSelection(position);
	}
	
	public void Remove(int position){
		int index = GetCurrentListViewIndex();
		_Views.get(index)._Items.Remove(position);
	}
	
	public void Clear(){
		int index = GetCurrentListViewIndex();
		_Views.get(index)._Items.Clear();
	}
	
	public void addThread(TwitterItem item){
		int index = GetCurrentListViewIndex();
		_Views.get(index)._Items.addThread(item);
	}
	
	public int GetCurrentThread(){
		int index = GetCurrentListViewIndex();
		return _Views.get(index)._CurrentThread;
	}
	
	public void SetCurrentThread(int position){
		int index = GetCurrentListViewIndex();
		 _Views.get(index)._CurrentThread = position;
	}
	
	public int GetCurrentListViewType(){
		int index = GetCurrentListViewIndex();
		return _Views.get(index)._ViewType;
	}
	
	public void SetCurrentListViewType(int type){
		int index = GetCurrentListViewIndex();
		_Views.get(index)._ViewType = type;
	}

	public void SetListViewType(int index, int type){
		_Views.get(index)._ViewType = type;
	}
	
	public int GetCurrentListViewCount(){
		int index = GetCurrentListViewIndex();
		return _Views.get(index)._TweetsCount;
	}
	
	public void SetCurrentListViewCount(int count){
		int index = GetCurrentListViewIndex();
		_Views.get(index)._TweetsCount = count;
	}
	
	public void SetListViewCount(int index, int count){
		_Views.get(index)._TweetsCount = count;
	}
}
